package com.demo.zyt.modules.app.service.impl;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.demo.zyt.common.dto.user.WxApplyAllianceDTO;
import com.demo.zyt.common.exception.RRException;
import com.demo.zyt.common.utils.AppResultEnum;
import com.demo.zyt.config.CommonParameters;
import com.demo.zyt.modules.app.dao.WxApplyAllianceDao;
import com.demo.zyt.modules.app.dao.WxUserDao;
import com.demo.zyt.modules.app.entity.WxApplyAllianceEntity;
import com.demo.zyt.modules.app.service.WxApplyAllianceService;


@Service("wxApplyAllianceService")
public class WxApplyAllianceServiceImpl extends ServiceImpl<WxApplyAllianceDao, WxApplyAllianceEntity> implements WxApplyAllianceService {

	@Autowired
	private WxUserDao wxUserDao;
	
	@Override
	public void save(WxApplyAllianceDTO wxApplyAllianceDTO) {
		
		if ( StringUtils.isEmpty( this.wxUserDao.selectById(wxApplyAllianceDTO.getUserid()).getMobile())) {
			throw new RRException(AppResultEnum.MUST_BIND_MOBILE);
		}
		WxApplyAllianceEntity queryEntity = new WxApplyAllianceEntity();
		queryEntity.setUserid(wxApplyAllianceDTO.getUserid());
		if ( this.baseMapper.selectOne(queryEntity) != null ) {
			throw new RRException(AppResultEnum.ALREADY_APPLY_ALLIANCE);
		}
		
		WxApplyAllianceEntity wxApplyAllianceEntity = new WxApplyAllianceEntity();
		
		wxApplyAllianceEntity.setCreatedDate(new Date());
		wxApplyAllianceEntity.setName(wxApplyAllianceDTO.getName());
		wxApplyAllianceEntity.setRemark(wxApplyAllianceDTO.getRemark());
		wxApplyAllianceEntity.setStatus(CommonParameters.IS_EFFECT.NO);
		wxApplyAllianceEntity.setUserid(wxApplyAllianceDTO.getUserid());
		wxApplyAllianceEntity.setExtInfo(wxApplyAllianceDTO.getExtInfo());
		
		wxApplyAllianceEntity.setOrgName(wxApplyAllianceDTO.getOrgName());
		wxApplyAllianceEntity.setProvinceId(wxApplyAllianceDTO.getProvinceId());
		wxApplyAllianceEntity.setCityId(wxApplyAllianceDTO.getCityId());
		wxApplyAllianceEntity.setRegionId(wxApplyAllianceDTO.getRegionId());
		
		this.baseMapper.insert(wxApplyAllianceEntity);
	}
	
}
